#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   APP_CommunicationControl_0x28_test.py
@Time    :   2025/04/03 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   UDS 0x28通信控制服务测试
'''

from pytest_assume.plugin import assume
import pytest
import logging
import time
from commDriver.TS_CAN import *
import allure
import sys
sys.dont_write_bytecode = True

# region 测试数据定义
communicationControl_NRC7F = [
    ("28 00 01", "7F 28 7F"),
    ("28 03 01", "7F 28 7F"),
    ("28 00 03", "7F 28 7F"),    
    ("28 03 03", "7F 28 7F"),
    ("28 80 01", "7F 28 7F"),
    ("28 83 01", "7F 28 7F"),
    ("28 80 03", "7F 28 7F"),
    ("28 02 03", "7F 28 7F"),       # 7F > 12
    ("28 83 03", "7F 28 7F"),       # 7F > 12
    ("28 00 01 00", "7F 28 7F"),    # 7F > 13
    ("28", "7F 28 7F"),             # 7F > 13
    ("28 00 05", "7F 28 7F"),       # 7F > 31
    ("28 80 01 00 00", "7F 28 7F"), # 7F > 31
    ("28 83 03 00", "7F 28 7F"),    # 7F > 31 
]
# endregion

# region 物理寻址测试
class Test_BOOT_CommunicationControl_Phy:
    """物理寻址模式测试"""
    @allure.severity('normal')
    @allure.title('物理寻址-编程会话-NRC7F测试')
    @allure.description('验证编程会话下应响应NRC7F')
    @pytest.mark.parametrize('req,resp', communicationControl_NRC7F, 
                           ids=[f"Req:{req}" for req, _ in communicationControl_NRC7F])
    def test_CommunicationControl_NRC7F_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

    @allure.severity('critical')
    @allure.title('物理寻址-编程会话-NRC7F/NRC22优先级验证')
    @allure.description('验证物理寻址编程会话下NRC响应顺序：7F > 22')
    def test_CommunicationControl_NRC7F_NRC22_Priority_Programming_Phy(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 5)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '28 00 01', '7F 28 7F', 0, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '28 00 01', '7F 28 7F', 0, iStep, False)
    # endregion
# endregion

# region 功能寻址测试
class Test_BOOT_CommunicationControl_Func:
    """功能寻址模式测试"""
    @allure.severity('normal')
    @allure.title('功能寻址-编程会话-NRC7F测试')
    @allure.description('验证编程会话下应响应NRC7F')
    @pytest.mark.parametrize('req,resp', communicationControl_NRC7F,
                           ids=[f"Req:{req}" for req, _ in communicationControl_NRC7F])
    def test_CommunicationControl_NRC7F_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, req, '', 2, iStep, False, 'UDSFun')

    @allure.severity('critical')
    @allure.title('功能寻址-编程会话-NRC11 NRC22优先级验证')
    @allure.description('验证功能寻址编程会话下NRC响应顺序：11 > 22')
    def test_CommunicationControl_NRC11_NRC22_Priority_Programming_Func(self, action, Driver):
        iStep = 1
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 0, iStep, False)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 5)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '28 00 01', '', 0, iStep, False, 'UDSFun')
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False)
        iStep = Driver['CAN'].SetVehicleSpeed(Driver, iStep, 0)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 0, iStep, False)
        iStep, _, _ = Driver['CAN'].checkReqResp(Driver, '28 00 01', '', 0, iStep, False, 'UDSFun')
# endregion